/*
 * @Author: your name
 * @Date: 2021-04-24 13:44:52
 * @LastEditTime: 2021-06-06 12:27:27
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /admin/src/pages/login/index.tsx
 */
import { Form, Input, Button, Checkbox } from 'antd';
import React from 'react';
import './index.less';
import { LockOutlined, UserOutlined } from '@ant-design/icons';
import { history } from 'umi';
import { timInit } from '@/components/tim/tim';
import { login } from '@/services/user';
import { observer } from 'mobx-react';
import { globalModel } from '../globalModel';
import { MenuList } from '../../routers/index';
import { Tabs } from 'antd';
import useObj from '@/use/useObj';
interface IProps {}

const Login = (props: IProps) => {
  const [obj, setObj] = useObj({
    activeKey: '1',
  });
  const onFinish = async (values: any) => {
    values.type = obj.activeKey;
    const res = await login(values);
    console.log(res, 'res=====');

    globalModel.userInfo = res.data.results;
    // timInit(values);
    const path = MenuList(
      globalModel.userInfo?.box_id,
      globalModel.userInfo?.is_super,
    )[0].path;
    history.push(path || '/');
  };

  const onFinishFailed = (errorInfo: any) => {
    console.log('Failed:', errorInfo);
  };

  return (
    <div className="login">
      <div>
        <div className="title">森意门店终端管理系统</div>
        <div className="container">
          <Tabs
            type="card"
            defaultActiveKey="1"
            centered
            onChange={(e) => setObj({ activeKey: e })}
          >
            <Tabs.TabPane tab="管理员登录" key="1"></Tabs.TabPane>
            <Tabs.TabPane tab="包厢登录" key="2"></Tabs.TabPane>
          </Tabs>
          <Form
            name="basic"
            size="large"
            initialValues={{ remember: true }}
            onFinish={onFinish}
            onFinishFailed={onFinishFailed}
          >
            <Form.Item
              name="username"
              rules={[{ required: true, message: '请输入用户名' }]}
            >
              <Input
                placeholder="请输入用户名"
                prefix={<UserOutlined className="site-form-item-icon" />}
              />
            </Form.Item>

            <Form.Item
              name="password"
              rules={[{ required: true, message: '请输入密码' }]}
            >
              <Input.Password
                placeholder="请输入密码"
                prefix={<LockOutlined className="site-form-item-icon" />}
              />
            </Form.Item>

            {/* <Form.Item name="remember" valuePropName="checked">
            <Checkbox>记住密码</Checkbox>
          </Form.Item> */}

            <Form.Item>
              <Button
                type="primary"
                htmlType="submit"
                style={{ width: '100%' }}
              >
                登录
              </Button>
            </Form.Item>
          </Form>
        </div>
      </div>
    </div>
  );
};
export default observer(Login);
